﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace XChipTool
{
    public partial class ScaTrackBar : DevExpress.XtraEditors.XtraUserControl
    {

        public delegate void SendEventScaTrackClick(int msg);   
        /// <summary>
        /// 发送theta值
        /// </summary>
        public  event SendEventScaTrackClick SendTheatClick;
        /// <summary>
        /// 发送alaha值
        /// </summary>
        public  event SendEventScaTrackClick SendAlphaClick;

        bool startMove = false;
        bool startSidesMove = false;
        int clickX = 0;
        int clickSidesMove = 0;

        int sidesNum = 0;
        //int AlphaScope = 0;
        //初始化全局变量系数       
        double coNum = 0;
        bool mouseUpMask = false;
        int zhubtn_x;
        bool isEnablePic = false;

        public ScaTrackBar()
        {
            InitializeComponent();
            //初始化全局变量系数
            coNum = (double)pictureEdit1.Width / 360;
            //maxW = (double)pictureEdit1.Width / (720 - this.zhubtn.Width);
        }

        public ScaTrackBar(int theat, int alpha) {
            sidesNum = alpha*2;
            SetScrollBarValue(theat, alpha);
        }

        public void ScaEnableToBool(bool isbool) {
            zuobtn.Enabled = isbool;
            youbtn.Enabled = isbool;
            zhubtn.Enabled = isbool;
            //pictureEdit1.Enabled = isbool;
            isEnablePic = isbool;
        }

        private void pictureEdit1_MouseClick(object sender, MouseEventArgs e)
        {
            if (isEnablePic)
            {
                //zhubtn.   e.X;
                //zhubtn.Location = new Point(e.X, 0);
                SetScrollBarValue((int)(e.X / coNum), (int)(sidesNum / coNum));
                if (null != SendTheatClick)
                {
                    mouseUpMask = true;//标志位，只传值，不重画；
                    SendTheatClick((int)(e.X / coNum));
                    mouseUpMask = false;
                }
            }
           
        }
       

        //int clickY = 0;
        private void zhubtn_MouseDown(object sender, MouseEventArgs e)
        {
            if (isEnablePic)
            {
                clickX = e.X;
                //clickY = e.Y;
                startMove = true;
            }
          
        }

        private void zhubtn_MouseUp(object sender, MouseEventArgs e)
        {
            if (isEnablePic)
            {
                startMove = false;
                if (null != SendTheatClick)
                {
                    //回调函数出去值  这里需要maxW 参与计算
                    //SendTheatClick(zhubtn_x/2);
                    mouseUpMask = true;//标志位，只传值，不重画；
                    SendTheatClick((int)(zhubtn_x / coNum));
                    mouseUpMask = false;
                }
            }
           
        }

        private void zhubtn_MouseMove(object sender, MouseEventArgs e)
        {
            if (!isEnablePic) return;
            if (startMove)
            {
                // e.X 是正负数,表示移动的方向
                 zhubtn_x = zhubtn.Location.X + e.X - clickX;   //还要减去上次鼠标点击的位置
                                                            //int tempA = zhubtn.Location.X - zuobtn.Location.X-zuobtn.Size.Width;

                //动态计算宽度位置
               
                var minW = this.zhubtn.Width; //按钮的宽度
                var tempMaxW = (pictureEdit1.Width-zhubtn.Width/2); //判断实际长度    （）
                if (zhubtn_x > tempMaxW)
                {
                    zhubtn_x = pictureEdit1.Width;
                    zhubtn.Location = new Point(tempMaxW, 0);
                }
                else if (zhubtn_x <0)
                {
                    zhubtn_x = 0;
                    zhubtn.Location = new Point(0, 0);
                }
                else
                {
                    zhubtn.Location = new Point(zhubtn_x, 0);
                }
                //if (null != SendTheatClick) //回调挪到mouse up里
                //{
                //    //回调函数出去值  这里需要maxW 参与计算
                //    //SendTheatClick(zhubtn_x/2);
                //    SendTheatClick((int)(zhubtn_x / coNum));
                //}
                int zx = zhubtn_x - sidesNum;
                int yx = zhubtn_x + zhubtn.Size.Width + sidesNum;
                if (zx > tempMaxW)
                {
                    //zuobtn.Location = new Point(zx - 720, 0);
                    zuobtn.Location = new Point((int)(zx - tempMaxW), 0);
                }
                else if (zx < 0)
                {
                    //zuobtn.Location = new Point(720 + zx, 0);
                    zuobtn.Location = new Point((int)(tempMaxW + zx), 0);
                }
                else
                {
                    zuobtn.Location = new Point(zx, 0);
                }
                //if (yx>720)
                if (yx > tempMaxW)
                {
                    //youbtn.Location = new Point(yx - 720, 0);
                    youbtn.Location = new Point((int)(yx - tempMaxW), 0);
                }
                else if (yx < 0)
                {
                    //youbtn.Location = new Point(720 + yx, 0);
                    youbtn.Location = new Point((int)(tempMaxW + yx), 0);
                }
                else
                {
                    youbtn.Location = new Point(yx, 0);
                }
            }
        }

        private void zuobtn_MouseDown(object sender, MouseEventArgs e)
        {
            if (!isEnablePic) return;
            clickSidesMove = e.X;
            startSidesMove = true;
        }

        private void zuobtn_MouseUp(object sender, MouseEventArgs e)
        {
            if (!isEnablePic) return;
            startSidesMove = false;
            if (null != SendAlphaClick)
            {
                //SendAlphaClick(Math.Abs(sidesNum) / 2);
                mouseUpMask = true;
                SendAlphaClick((int)(Math.Abs(sidesNum) / coNum));
                mouseUpMask = false;
            }
        }

        private void zuobtn_MouseMove(object sender, MouseEventArgs e)
        {
            if (!isEnablePic) return;
            if (startSidesMove)
            {

                // e.X 是正负数,表示移动的方向
                int x = zuobtn.Location.X + e.X - clickX;   //还要减去上次鼠标点击的位置
                sidesNum = zhubtn.Location.X - x - zuobtn.Size.Width;
                // if (sidesNum > 120)  120 是值宽度值* 2  实际值60 
                //动态计算宽度位置
                var maxW = (pictureEdit1.Width - zhubtn.Width / 2);
                var minW = this.zhubtn.Width; //按钮的宽度
                var tempMaxW = (int)(coNum * 360); //判断实际长度    （）
                var tempMaxSW = (int)(coNum* 60); //判断实际左右两边宽度长度    （）
                                              //if (sidesNum > 120)
                if (sidesNum > tempMaxSW)
                {
                    sidesNum = tempMaxSW;
                    x = zhubtn.Location.X - sidesNum - zuobtn.Size.Width;
                }
                if (sidesNum < (tempMaxSW * -1))
                {
                    sidesNum = (tempMaxSW * -1);
                    x = zhubtn.Location.X - sidesNum - zuobtn.Size.Width;
                }
              
                int xy = zhubtn.Location.X + zhubtn.Size.Width + sidesNum;
                if (x > tempMaxW)
                {
                    zuobtn.Location = new Point(x - tempMaxW, 0);
                }
                else if (x < 0)
                {
                    zuobtn.Location = new Point(tempMaxW + x, 0);
                }
                else
                {
                    zuobtn.Location = new Point(x, 0);
                }
                if (xy > tempMaxW)
                {
                    youbtn.Location = new Point(xy - tempMaxW, 0);
                }
                else if (xy < 0)
                {
                    youbtn.Location = new Point(tempMaxW + xy, 0);
                }
                else
                {
                    youbtn.Location = new Point(xy, 0);
                }
            }
        }  
            
        

        private void youbtn_MouseDown(object sender, MouseEventArgs e)
        {
            if (!isEnablePic) return;
            clickSidesMove = e.X;
            startSidesMove = true;
        }

        private void youbtn_MouseUp(object sender, MouseEventArgs e)
        {
            if (!isEnablePic) return;
            startSidesMove = false;
            if (null != SendAlphaClick)
            {
                mouseUpMask = true;
                SendAlphaClick((int)(Math.Abs(sidesNum) / coNum));
                mouseUpMask = false;
            }
        }

        private void youbtn_MouseMove(object sender, MouseEventArgs e)
        {
            if (!isEnablePic) return;
            if (startSidesMove)
            {
                // e.X 是正负数,表示移动的方向
                int x = youbtn.Location.X + e.X - clickX;   //还要减去上次鼠标点击的位置
                sidesNum = x - zhubtn.Location.X - zhubtn.Size.Width;
                var maxW = (pictureEdit1.Width - zhubtn.Width / 2);
                var minW = this.zhubtn.Width; //按钮的宽度
                var tempMaxW = (int)(coNum * 360); //判断实际长度    （）
                var tempMaxSW = (int)(coNum * 60); //判断实际左右两边宽度长度    （）
                if (sidesNum> tempMaxSW)
                {
                    sidesNum = tempMaxSW;
                    x = zhubtn.Location.X + sidesNum + zhubtn.Size.Width;
                }
                if (sidesNum<-tempMaxSW)
                {
                    sidesNum = -tempMaxSW;
                    x = zhubtn.Location.X + sidesNum + zhubtn.Size.Width;
                }
              
                int xy = zhubtn.Location.X - zuobtn.Size.Width - sidesNum;
                if (x > tempMaxW)
                {
                    youbtn.Location = new Point(x - tempMaxW, 0);
                }
                else if (x < 0)
                {
                    youbtn.Location = new Point(tempMaxW + x, 0);
                }
                else
                {
                    youbtn.Location = new Point(x, 0);
                }

                if (xy > tempMaxW)
                {
                    zuobtn.Location = new Point(xy - tempMaxW, 0);
                }
                else if (xy < 0)
                {
                    zuobtn.Location = new Point(tempMaxW + xy, 0);
                }
                else
                {
                    zuobtn.Location = new Point(xy, 0);
                }

            }
        }
        /// <summary>
        /// 初始化设置变量位置
        /// </summary>
        /// <param name="zhuNum"></param>
        /// <param name="sidesNum"></param>
        public void SetScrollBarValue(int zhuNum,int _sidesNum) {
            if (!isEnablePic) return;
            int tempNum = (int)(zhuNum * coNum);
            sidesNum = (int)(_sidesNum * coNum);
            var maxWidth = pictureEdit1.Width - zhubtn.Width/2;
            if (tempNum>maxWidth)
            {
                tempNum = maxWidth;
            }
            else if (tempNum<0)
            {
                tempNum = 0;
            }
            if (!mouseUpMask)//判断是否需要重绘
            {
                zhubtn.Location = new Point(tempNum, 0);//Center

                int tempZNum = tempNum - zuobtn.Size.Width - sidesNum;
                if (tempZNum > pictureEdit1.Width)
                {
                    zuobtn.Location = new Point(tempZNum - pictureEdit1.Width, 0);
                }
                else if (tempZNum < 0)
                {
                    zuobtn.Location = new Point(pictureEdit1.Width + tempZNum, 0);
                }
                else
                {
                    zuobtn.Location = new Point(tempZNum, 0);
                }

                int tempYNum = tempNum + sidesNum + zhubtn.Size.Width;
                if (tempYNum > pictureEdit1.Width)
                {
                    youbtn.Location = new Point(tempYNum - pictureEdit1.Width, 0);
                }
                else if (tempYNum < 0)
                {
                    youbtn.Location = new Point(pictureEdit1.Width + tempYNum, 0);
                }
                else
                {
                    youbtn.Location = new Point(tempYNum, 0);
                }
            }
          
        }
    }
}
